#include "instructions/references/instance_of.hpp"
#include "rtda/frame.h"
#include "rtda/heap/cp_classref.h"

namespace jvm {
namespace instructions {

void InstanceOf::Execute(Frame& frame) {
    auto stack = frame.GetOperandStack();
    auto ref = stack->PopRef();
    
    if (!ref) {
        stack->PushInt(0);
        return;
    }

    auto cp = frame.GetMethod()->GetClass()->GetConstantPool();
    auto classRef = std::dynamic_pointer_cast<ClassRef>(cp->GetConstant(index_));
    auto clazz = classRef->ResolvedClass();
    
    if (ref->IsInstanceOf(clazz)) {
        stack->PushInt(1);
    } else {
        stack->PushInt(0);
    }
}

} // namespace instructions
} // namespace jvm